﻿#include "CItem.h"

#include "../Utils/Utils.h"

#include "Tag.h"

C_BaseActorRenderContext::C_BaseActorRenderContext(C_ScreenContext *ScreenCtx, C_ClientInstance *client, MinecraftGame *game) {
	fast_io::freestanding::my_memset(this, 0, sizeof(C_BaseActorRenderContext));
	using BaseActorRenderContext_t = __int64(__fastcall *)(C_BaseActorRenderContext *, C_ScreenContext *, C_ClientInstance *, MinecraftGame *);
	static BaseActorRenderContext_t BaseActorRenderContext_constructor = reinterpret_cast<BaseActorRenderContext_t>(Utils::Utils::getBase() + 13419296);
	BaseActorRenderContext_constructor(this, ScreenCtx, client, game);
}

void C_ItemRenderer::renderGuiItemNew(C_BaseActorRenderContext *BaseActorRenderCtx, C_ItemStack *item, int mode, float x, float y, float opacity, float scale, bool isEnchanted) {
	using renderGuiItemNew_t = void(__fastcall *)(C_ItemRenderer *, C_BaseActorRenderContext *, C_ItemStack *, int, float, float, bool, float, float, float);
	static renderGuiItemNew_t renderGuiItemNew = reinterpret_cast<renderGuiItemNew_t>(Utils::Utils::getBase() + 15399584);
	item->setShowPickUp(false);
	renderGuiItemNew(this, BaseActorRenderCtx, item, mode, x, y, isEnchanted, 1.f, opacity, scale);
}

void C_ItemRenderer::renderGuiItemInChunk(C_BaseActorRenderContext *BaseActorRenderCtx, C_ItemStack *item, float x, float y, float opacity, float scale, bool isEnchanted, int mode) {
	using renderGuiItemInChunk_t = void(__fastcall *)(C_ItemRenderer *, C_BaseActorRenderContext *, __int64, C_ItemStack *, float, float, float, float, float, int);
	static renderGuiItemInChunk_t renderGuiItem = reinterpret_cast<renderGuiItemInChunk_t>(Utils::Utils::getBase() + 15402704);
	// item->setShowPickUp(false);

	renderGuiItem(this, BaseActorRenderCtx, 2i64, item, x, y, 1.f, 1.f, scale, 0);
}

C_ItemStack::C_ItemStack(const Tag &tag) {
	fast_io::freestanding::my_memset(this, 0x0, sizeof(C_ItemStack));
	setVtable();
	fromTag(tag);
}

void C_ItemStack::fromTag(const Tag &tag) {
	using ItemStackBase__loadItemF = void(__fastcall *)(C_ItemStack *, Tag const &);
	static ItemStackBase__loadItemF fromTag = reinterpret_cast<ItemStackBase__loadItemF>(Utils::Utils::getBase() + 37513904);
	fromTag(this, tag);
}

void C_ItemStack::save(CompoundTag **tag) {
	using ItemStackBase__saveF = void(__fastcall *)(C_ItemStack *, CompoundTag **);
	ItemStackBase__saveF save = reinterpret_cast<ItemStackBase__saveF>(Utils::Utils::getBase() + 37539664);
	save(this, tag);
}

void C_ItemStack::setUserData(std::unique_ptr<Tag> tag) {
	using setUserData_t = void(__fastcall *)(C_ItemStack *, std::unique_ptr<Tag>);
	setUserData_t setUserData = reinterpret_cast<setUserData_t>(Utils::Utils::getBase() + 37523088);
	setUserData(this, std::move(tag));
}

void C_ItemStack::reinit(C_BlockLegacy &legacy, int count) {
	this->setVtable();
	Utils::Utils::CallVFunc<2, void>(this, &legacy, count);
}

void C_ItemStack::reinit(C_Item &item, int count, int itemData) {
	this->setVtable();
	Utils::Utils::CallVFunc<3, void>(this, &item, count, itemData);
}

int C_ItemStack::getEnchantValue(int enchantId) {
	using getEnchantsLevel_t = int(__fastcall *)(int, C_ItemStack *);
	static getEnchantsLevel_t getEnchantsLevel = reinterpret_cast<getEnchantsLevel_t>(Utils::Utils::getBase() + 42305808);
	return getEnchantsLevel(enchantId, this);
}

void C_ItemStack::setVtable(void) {
	static uintptr_t sigOffset = Utils::Utils::getBase() + 4425504;
	int offset = *reinterpret_cast<int *>(sigOffset + 3);
	this->vTable = reinterpret_cast<uintptr_t **>(sigOffset + offset + /*length of instruction*/ 7);
}

C_Item ***ItemRegistry::getItemFromId(void *ptr, int itemId) {
	using getItemFromId_t = C_Item ***(__fastcall *)(void *, int);
	static getItemFromId_t getItemFromId = reinterpret_cast<getItemFromId_t>(Utils::Utils::getBase() + 36586768);
	return getItemFromId(ptr, itemId);
}

C_Item ***ItemRegistry::lookUpByName(void *a1, void *a2, TextHolder &text) {
	using ItemRegistry__lookupByName_t = C_Item ***(__fastcall *)(void *, void *, TextHolder &);
	static ItemRegistry__lookupByName_t ItemRegistry__lookupByNameF = reinterpret_cast<ItemRegistry__lookupByName_t>(Utils::Utils::getBase() + 36600448);
	return ItemRegistry__lookupByNameF(a1, a2, text);
}

ItemDescriptor::ItemDescriptor(int id, int16_t itemData) {
	using ItemDescriptor__ItemDescriptor_t = ItemDescriptor *(__fastcall *)(ItemDescriptor *, int, int16_t);
	static ItemDescriptor__ItemDescriptor_t func = reinterpret_cast<ItemDescriptor__ItemDescriptor_t>(Utils::Utils::getBase() + 31219584);
	func(this, id, itemData);
}